<?php

class SearchController extends BaseController 
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $arrGet=$this->getRequest()->getParams();
               
        $type=isset($arrGet['type'])?$arrGet['type']:$this->type;
        $sWhere=' AND m.active=1 AND m.type='.$type;
        if(isset($arrGet['manufacture']) && $arrGet['manufacture']!=''){
            $manufacture = $this->view->ALIAS_MANUFACTURE[$arrGet['manufacture']];
            $sWhere.=' AND m.manu_id='.$manufacture['id'];
        }
         if(isset($arrGet['model']) && $arrGet['model']!=''){
            //$model = $this->view->ALIAS_MODEL[$arrGet['model']];
            $mMoto = new Motor();
            $id=$mMoto->getModelIdByAlias($arrGet['model']);
            $sWhere.=' AND m.model_id='.$id;
            unset($mMoto);
            //var_dump($arrGet['model']); die();
        } 
        if(isset($arrGet['style']) && $arrGet['style']!=''){
            $style = $this->view->ALIAS_STYLE[$arrGet['style']];
            $sWhere.=' AND m.style_id='.$style['id'];
        }
        if(isset($arrGet['city']) && $arrGet['city']!=''){
            $city = $this->view->ALIAS_CITY[$arrGet['city']];
            $sWhere.=' AND m.city_id='.$city['id'];
        }
        if(isset($arrGet['price']) && $arrGet['price']!=''){
            list ($from, $to) =explode('-',$arrGet['price']);
            
            $from = $from *1000000;
            $to = $to *1000000;
            
            if ($to==0){
                $sWhere.=' AND m.price>='.$from;
            }elseif($from==0){
                $sWhere.=' AND (m.price>0 AND m.price<'.$to.')';
            }else{                
                $sWhere.=' AND (m.price>='.$from.' AND m.price<'.$to.')';
            }
            
        }
        #var_dump($sWhere);
        #$sWhere=' AND m.manu_id AND m.model_id AND m.style_id AND m.city_id='.$arrGet['city'];
		$mMotorSearch = new MotorSearch();
        $page = $this->_request->getParam('page',1);
    	$page = $page ? intval($page):1;
    	$rows_per_page = 10; 
    	$offset = ($page-1) * $rows_per_page;
        $arrMotors = $mMotorSearch->getMotors($sWhere, 'ORDER BY m.created_time DESC', $offset, $rows_per_page);
        $html ='';
        $sClass='';
        $i=0;
			
		
        $iCount=count($arrMotors['result']);
        if($iCount>0){
            foreach ($arrMotors['result'] as $row){
                if($row['image'] && file_exists(UPLOAD.'default/item/'.$row['image']) ){
                    $this->view->image = UPLOAD_DIR.'default/item/'.$row['image'];
                }else{
                    $this->view->image = IMAGE_URL.'no_img.png';
                }
                 /* if(($row['manu_id']<1)&&($this->type==0)) $this->view->name = "Xe máy cũ";
				else if (($row['manu_id']<1)&& ($this->type==1)) $this->view->name = "Ô tô cũ";
				else */ 
				if ($row['manu_name']=="Khác" && $row['manu_khac']!="Thương hiệu xe của bạn là gì?") $row['manu_name']=$row['manu_khac'];
				if ($row['model_name']=="Khác" && $row['model_khac']!="Model xe của bạn là gì? ") $row['model_name']=$row['model_khac'];
				$this->view->name = $row['manu_name'] . ' '.$row['model_name'];
				if ($this->view->name=="Khác Khác"&&($this->type==0)) $this->view->name = "Xe máy cũ";
				else if ($this->view->name=="Khác Khác"&&($this->type==1)) $this->view->name = "Ô tô cũ";
                $this->view->date = date('d/m/Y', strtotime($row['created_time']));
                if ($row['price']){
                   // $this->view->price = $row['price'] . ' Triệu';
                   $this->view->price = number_format($row['price'],0,'','.') . ' VNĐ';
                }else{
                    //$this->view->price = $row['price_from'] .' Triệu - '. $row['price_to'].' Triệu';
                    $this->view->price = ' Thỏa thuận';
                }
                
                $this->view->city = $row['city_name'];
                $this->view->brand = $row['manu_name'];
                if(($iCount%2==0 && ($iCount-$i)<=2) || ($iCount%2!=0 && ($iCount-$i)<=1)){
                    $sClass='listLast';
                }
                $i++;
                $this->view->SCLASS=$sClass;
                
                $link = $this->view->translate('by_type_detail_'.$this->type);
                $link1 = Common::urlParser(array ($row['alias']), $link).'.html';
                
                $link1 = str_replace ('([0-9]+)',$row['id'],$link1); 
                
                $this->view->link='/'.$link1;
            
                $html .= $this->view->render('search/item.phtml');
            }
            
            $this->view->LIST = $html;
            $this->view->TOTAL=$arrMotors['total'];
            
//          $pager = LoadObject::startPaging();
//        	$sPaging_html = $pager->pager($arrMotors['total'], $rows_per_page, $page, 'main/paging.phtml');
            $pager = new Defined_PageBreak();
            $maxpage = ceil ($arrMotors['total']/$rows_per_page);
            $sPaging_html = $pager->wp_page_numbers($maxpage, $page);
            
        	$this->view->PAGINATOR = $sPaging_html;
        } else {
		
            $this->view->LIST='<center>Không có kết quả phù hợp</center>';
            
        }
        
    }

    function listAction(){
        $type=$this->type; //1:oto 0:moto
        
        $mMotorSearch = new MotorSearch();
        
        $city = Common::getString(null,'city');
        $brand = Common::getString(null,'brand');
        
        $from= Common::getInt(null, 'from');
        $to= Common::getInt(null, 'to');
        
        $price = $this->view->ALIAS_PRICE[$from.'-'.$to]['id'];        
        
        $this->view->page = $this->getRequest()->getParam('page',1);  
                     
        $strwhere = ' AND m.active=1 AND m.type='.$type;
        if ($city){
            $data = $this->view->ALIAS_CITY[$city] ;//$mMotorSearch->getDataByAlias('city',$city);
            
            $strwhere .= ' AND m.city_id='.$data['id'];
            $city_name = $data['name'];
            $this->view->ct = $city;
            
            $this->view->H1 = str_replace ('(city)', $city_name, $this->view->H1);
            $this->page_title = str_replace ('(city)', $city_name, $this->page_title);
            $this->page_des = str_replace ('(city)', $city_name, $this->page_des);
            
            $key_name = array($data['alias']);
            
            $link = $this->view->translate('by_city_detail_'.$this->type);
            
        }elseif($brand){
            $data = $this->view->ALIAS_MANUFACTURE[$brand] ;//$mMotorSearch->getDataByAlias('manufacture',$brand);
            
            $strwhere .= ' AND m.manu_id='.$data['id'];
            $brand_name = $data['name'];
            $this->view->brd = $brand;
            $this->view->H1 = str_replace ('(brand)', $brand_name, $this->view->H1);
            $this->page_title = str_replace ('(brand)', $brand_name, $this->page_title);
            $this->page_des = str_replace ('(brand)', $brand_name, $this->page_des);
            
            $key_name = array ($data['alias']);
            $link = $this->view->translate('by_brand_detail_'.$this->type);
        }elseif ($price){ 
            
            $price_name = $this->view->ALIAS_PRICE[$from.'-'.$to]['display'];
            
            $from = $from *1000000;
            $to = $to *1000000;
            
            if ($to==0){
                $strwhere.=' AND m.price>='.$from;
            }elseif($from==0){
                $strwhere.=' AND (m.price>0 AND m.price<'.$to.')';
            }else{                
                $strwhere.=' AND (m.price>='.$from.' AND m.price<'.$to.')';
            }
            /*if($to==0){
                $strwhere.=' AND (m.price>='.$from.' OR m.price_from>='.$from.')';
            } elseif($from==0){
                $strwhere.=' AND ( (m.price>0 AND m.price<='.$to.') OR ( m.price_to>0 AND m.price_to <='.$to.'))';
            } else{
                $strwhere.=' AND ((m.price BETWEEN '.$from.' AND '.$to.')
                    OR (m.price_from>='.$from.' AND m.price_to <='.$to.')
                    OR (m.price_from='.$from.' AND m.price_to >='.$to.')
                    OR (m.price_from<='.$from.' AND m.price_to ='.$to.')
                )';
            } */
                
            
            
            $this->view->pri = $price;
            
            $this->view->H1 = str_replace ('(price)', $price_name, $this->view->H1);
            $this->page_title = str_replace ('(pricename)', $price_name, $this->page_title);
            $this->page_des = str_replace ('(price)', $price_name, $this->page_des);
            
            $key_name = array ($from, $to);
            $link = $this->view->translate('by_price_detail_'.$this->type);
            
        }
        
        $request = $this->getRequest();
        if ($request->isPost()){
            $icity = $request->getPost('icity','');
            $ibrand = $request->getPost('ibrand','');
            $iprice = $request->getPost('iprice','');
            
            
            $this->view->icity = $icity;    
            $this->view->ibrand = $ibrand; 
            $this->view->iprice = $iprice;
            
            if ($icity!=''){
                $strwhere .= ' AND m.city_id='.$icity;
                         
            }
            if($ibrand!=''){
                $strwhere .= ' AND m.manu_id='.$ibrand;
                   
                       
            }
            if ($iprice!=''){
                
                list ($from, $to) = explode ('-', $iprice);
                
                $from = $from *1000000;
                $to = $to *1000000;
            
                if ($to==0){
                    $strwhere.=' AND m.price>='.$from;
                }elseif($from==0){
                    $strwhere.=' AND (m.price>0 AND m.price<'.$to.')';
                }else{                
                    $strwhere.=' AND (m.price>='.$from.' AND m.price<'.$to.')';
                }
            
                /*if($to==0){
                    $strwhere.=' AND (m.price>='.$from.' OR m.price_from>='.$from.')';
                } elseif($from==0){
                    $strwhere.=' AND ((m.price>0 AND m.price<='.$to.') OR (m.price_to>0 AND m.price_to <='.$to.'))';
                } else{
                    $strwhere.=' AND ((m.price BETWEEN '.$from.' AND '.$to.')
                        OR (m.price_from>='.$from.' AND m.price_to <='.$to.')
                        OR (m.price_from='.$from.' AND m.price_to >='.$to.')
                        OR (m.price_from<='.$from.' AND m.price_to ='.$to.')
                    )';
                }*/
            
                
                
            }
            
        }
        
        
        $page = $this->_request->getParam('page',1);
    	$page = $page ? intval($page):1;
    	$rows_per_page = 10; 
    	$offset = ($page-1) * $rows_per_page; 	
        
        $list_motors = $mMotorSearch->getMotors($strwhere, 'ORDER BY m.created_time DESC', $offset, $rows_per_page);        
        $sClass='';
        $i=0;
        $iCount=count($list_motors['result']);       
        $html ='';
        foreach ($list_motors['result'] as $row){
            
            if($row['image'] && file_exists(UPLOAD.'default/item/'.$row['image']) ){
                $this->view->image = UPLOAD_DIR.'default/item/'.$row['image'];
            }else{
                $this->view->image = IMAGE_URL.'no_img.png';
            }
            $this->view->name = $row['manu_name'] . ' '.$row['model_name'];
            $this->view->date = date('d/m/Y', strtotime($row['created_time']));            
            
            if ($row['price']){
                 // $this->view->price = $row['price'] . ' Triệu';
                $this->view->price = number_format($row['price'],0,'','.') . ' VNĐ';
            }else{
                 //$this->view->price = $row['price_from'] .' Triệu - '. $row['price_to'].' Triệu';
                $this->view->price = 'Thỏa thuận';
            }
                
            $this->view->city = $row['city_name'];
            $this->view->brand = $row['manu_name'];
            if(($iCount%2==0 && ($iCount-$i)<=2) || ($iCount%2!=0 && ($iCount-$i)<=1)){
                $sClass='listLast';
            }
            $i++;
            $this->view->SCLASS=$sClass;
            
            $key_name1 = $key_name;            
            array_push($key_name1, $row['alias']);
            
             
            $link1 = Common::urlParser($key_name1 , $link);
                               
            $link1 = str_replace ('([0-9]+)',$row['id'],$link1);                               
            $this->view->link='/'.$link1.'.html';
            
            $html .= $this->view->render('search/item.phtml');
        }
        
        if ($html){
            $this->view->LIST = $html;
        
            $pager = LoadObject::startPaging();
    	    $sPaging_html = $pager->pager($list_motors['total'], $rows_per_page, $page, 'search/paging.phtml');
    	
    	    $this->view->PAGINATOR = $sPaging_html;
        }else{
            $this->view->LIST = 'Không tìm thấy';
        }
        
    }
}

